package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.bean.PinCardBean;
import com.wwwarehouse.contract.program_operation.ArithUtil;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

import static contract.wwwarehouse.com.contract.R.layout.rescource_factort_pincard_item;

/**
 * Created by feng.gao on 2017/9/19.
 */

public class PinCardAdapter extends BaseAdapter {
	private List<ChooseModuleBean.ModuleList.BeanList> list = new ArrayList<>();
	private Context context;
	private int height;
	private int layout;
	private int topCount;


	public PinCardAdapter(List<ChooseModuleBean.ModuleList.BeanList> list, Context context, int height , int layout) {
		this.list = list;
		this.context = context;
		this.height = height;
		this.layout = layout;
	}

	public PinCardAdapter(List<ChooseModuleBean.ModuleList.BeanList> list, Context context, int height , int layout , int topCount) {
		this.list = list;
		this.context = context;
		this.height = height;
		this.layout = layout;
		this.topCount = topCount;
	}

	@Override
	public int getCount() {
		if (list.size() == 0) {
			return 1;
		} else if (list.size() >= 8) {
			return 8;
		}
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = View.inflate(context, layout, null);
			viewHolder = new ViewHolder();
			viewHolder.client_tv = (TextView) convertView.findViewById(R.id.client_tv);
			viewHolder.client_iv = (ImageView) convertView.findViewById(R.id.client_iv);
			viewHolder.mPincardLayout = (RelativeLayout) convertView.findViewById(R.id.pin_card_item_bg);
			if (layout == R.layout.client_pincard_item) {
				viewHolder.mClientpercentTv = (TextView) convertView.findViewById(R.id.percent_tv);
			}
			if (layout == R.layout.supplier_pincard_item) {
				viewHolder.mSupplyPercentTv = (TextView) convertView.findViewById(R.id.percent_tv);
			}
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}


		if (list != null) {
//			viewHolder.client_tv.setText(list.get(position).getTitle());
			if (list.size() > 0) {
				if (layout == R.layout.client_pincard_item) {
					if (list.size() == 1 && !list.get(position).isShowPercent()) {
						viewHolder.mClientpercentTv.setVisibility(View.GONE);
					} else {
						viewHolder.mClientpercentTv.setText(list.get(position).getProgress() + "%");
					}
				} else if (layout == R.layout.supplier_pincard_item) {
					if (list.size() == 1 && !list.get(position).isShowPercent()) {
						viewHolder.mSupplyPercentTv.setVisibility(View.GONE);
					} else {
						viewHolder.mSupplyPercentTv.setText(list.get(position).getProgress() + "%");
					}
				}
				if (topCount != 0 && layout == R.layout.rescource_factort_pincard_item) {
					if (topCount == 1 && position == 0) {
						viewHolder.mPincardLayout.setBackground(context.getResources().getDrawable(R.drawable.rescource_factor_background));
					} else if (topCount == 2 ) {
						if (position == 0 || position == 1) {
							viewHolder.mPincardLayout.setBackground(context.getResources().getDrawable(R.drawable.rescource_factor_background));
						}
					}else if (topCount == 3 ) {
						if (position == 0 || position == 1 || position == 2) {
							viewHolder.mPincardLayout.setBackground(context.getResources().getDrawable(R.drawable.rescource_factor_background));
						}
					}
				}
				viewHolder.client_tv.setText(list.get(position).getName());
				if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getSelectedUrl() != null) {
					if (layout == R.layout.client_pincard_item || layout == R.layout.supplier_pincard_item) {
						BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.client_iv);
					} else {
						BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getSelectedUrl(), viewHolder.client_iv);
					}
				} else {
					if (layout == R.layout.client_pincard_item) {
						viewHolder.client_iv.setImageResource(R.drawable.customer);
					} else if (layout == R.layout.rescource_pincard_item) {
						viewHolder.client_iv.setImageResource(R.drawable.resources);
					} else if (layout == R.layout.rescource_factort_pincard_item) {
						viewHolder.client_iv.setImageResource(R.drawable.resource_factors);
					} else if (layout == R.layout.supplier_pincard_item) {
						viewHolder.client_iv.setImageResource(R.drawable.supplier);
					}
				}

			}

			//均分的布局
			if (layout == R.layout.rescource_pincard_item || layout == R.layout.rescource_factort_pincard_item) {
				if (list.size() > 8) {
					AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, (int) ArithUtil.div(height , 8));
					convertView.setLayoutParams(lp);
					if (position == 7) {
						viewHolder.client_iv.setVisibility(View.GONE);
						viewHolder.client_tv.setText("...");
					}
				} else if (list.size() == 0) {
					AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, (int) (height ));
					convertView.setLayoutParams(lp);
				} else {
					AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
							LinearLayout.LayoutParams.MATCH_PARENT, (int) ArithUtil.div(height , list.size()));
					convertView.setLayoutParams(lp);

				}

			} else {
				if (list.size() > 0) {
					if (list.size() == 1) {
						AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
								LinearLayout.LayoutParams.MATCH_PARENT, (int) ArithUtil.mul(list.get(position).getProgress(), ArithUtil.div(height, 100)));

						convertView.setLayoutParams(lp);
					} else {
						if (list.get(position).getScale() <= 20) {
							viewHolder.client_iv.setVisibility(View.GONE);
						} else {
							viewHolder.client_iv.setVisibility(View.VISIBLE);
						}
						AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
								LinearLayout.LayoutParams.MATCH_PARENT, (int) ArithUtil.mul(list.get(position).getScale() , ArithUtil.div(height, 100)));

						convertView.setLayoutParams(lp);
					}

				}

			}

		}
		return convertView;
	}

	class ViewHolder {
		TextView client_tv;
		ImageView client_iv;
		RelativeLayout mPincardLayout;
		TextView mClientpercentTv;
		TextView mSupplyPercentTv;

	}
}
